package com.binary.coder.gift.core.interfaces.sys.facade;

import com.binary.coder.gift.core.common.bean.BizException;
import com.binary.coder.gift.core.common.bean.ResultBean;
import com.binary.coder.gift.core.common.excel.ExcelHttpExporter;
import com.binary.coder.gift.core.common.utils.SysApiSqlUtils;
import com.binary.coder.gift.core.domain.sys.ISysApiSqlService;
import com.binary.coder.gift.core.infrastructure.po.sys.SysApiSql;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * @author weitao
 */
@Slf4j
@ApiSort(2)
@Api(tags = "公共-前端调用执行SQL接口")
@RestController()
@RequestMapping("/api/sys/api")
public class SysApiSqlController {

    @Resource
    protected ISysApiSqlService sysApiSqlService;

    @PostMapping("/executeQueryById")
    @ApiOperation("执行接口查询数据")
    @ApiOperationSupport(order = 1)
    public ResultBean executeQueryById(long id, @RequestBody Map<String, Object> param) throws BizException {
        return ResultBean.success(sysApiSqlService.queryDataById(id, param));
    }

    @PostMapping("/executeQueryByCode")
    @ApiOperation("执行接口查询数据")
    @ApiOperationSupport(order = 2)
    public ResultBean executeQueryByCode(String code, @RequestBody Map<String, Object> param) throws BizException {
        return ResultBean.success(sysApiSqlService.queryDataByCode(code, param));
    }

    @PostMapping("/validSql")
    @ApiOperation("校验接口")
    @ApiOperationSupport(order = 3)
    public ResultBean<String> validSql(String code, @RequestBody Map<String, Object> param) throws BizException {
        return ResultBean.success().setData(sysApiSqlService.validSql(sysApiSqlService.getByCode(code), param));
    }

    @PostMapping("/exportByCode")
    @ApiOperation("执行接口查询数据")
    @ApiOperationSupport(order = 4)
    public void exportByCode(String code, HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Object> param) throws UnsupportedEncodingException, BizException {
        SysApiSql bean = sysApiSqlService.getByCode(code);
        SysApiSqlUtils.valSec(bean.getSelectSql());

        String sql = bean.getSelectSql();
        List<String> header = SysApiSqlUtils.getResultCellAsList(sql);
        Map headerAlias = bean.getCellAlias();
        String fileName = bean.getName();

        ExcelHttpExporter excelExporter = ExcelHttpExporter.create().init(request, response, fileName, header, headerAlias);
        excelExporter.initDefaultConverter().initDefaultWriteHandler().buildWriter();
        sysApiSqlService.exportExcelBySql(excelExporter, bean.getId(), bean.getSelectSql(), bean.getScript(), param);

    }
}
